package com.caishi.lkx.exam.api;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.caishi.lkx.common.AdminBaseApi;
import com.caishi.lkx.exam.model.ProductCodeModel;
import com.caishi.lkx.exam.service.*;
import com.caishi.lkx.user.model.AgentUserModel;
import com.caishi.lkx.user.model.UserModel;
import com.caishi.lkx.user.service.IAgentUserService;
import com.caishi.lkx.user.service.IUserService;
import com.zzw.common.Wrappers;

import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Tag(name = "产品激活码管理")
@Slf4j
@RestController
@RequestMapping("/exam/product/code")
public class ProductCodeApi extends AdminBaseApi<IProductCodeService, ProductCodeModel, String> {
    @Resource
    private IProductCodeService productCodeService;
    @Resource
    private IUserService<UserModel> userService;
    @Resource
    private IPackageSkuService skuService;
    @Resource
    private IPackageService packageService;
    @Resource
    private IExamService examService;

    @Resource
    private IIndustryService industryService;
    @Resource
    private IAgentUserService agentUserService;

    @Override
    public IProductCodeService loadService() {
        return productCodeService;
    }

    @GetMapping("detail/{id}")
    public ProductCodeModel detail(@PathVariable String id) {
       var code = productCodeService.getAndCheckByCode(id);
       code.setPackageName(packageService.queryName(code.getPackageId()));
       return code;
    }

    @Override
    protected LambdaQueryWrapper<ProductCodeModel> baseWrapper(ProductCodeModel model, JSONObject otherParam) {
        String createdBy = otherParam.getStr("createdBy");
        Boolean all = otherParam.getBool("all", false);
        List<String> ids = new ArrayList<>();
        if(StrUtil.isNotBlank(createdBy)){
            ids.add(createdBy);
        }
        if (all) {
            List<String> idList = agentUserService.selectList(Wrappers.<AgentUserModel>lambdaQuery().eq(AgentUserModel::getParentId, createdBy).select(AgentUserModel::getId)).parallelStream().map(AgentUserModel::getId).toList();
            ids.addAll(idList);
        }
        return super.baseWrapper(model.clearEmptyStringField("createdBy"), otherParam).in(!ids.isEmpty(), ProductCodeModel::getCreatedBy, ids);
    }

    @Override
    protected List<ProductCodeModel> listCover(List<ProductCodeModel> result, JSONObject otherParam) {
        return result.parallelStream().peek(r -> {
            if (StrUtil.isNotBlank(r.getSkuId())) {
                r.setSkuName(skuService.queryName(r.getSkuId()));
            }
            if (StrUtil.isNotBlank(r.getPackageId())) {
                r.setPackageName(packageService.queryName(r.getPackageId()));
            }
            r.setCreatedByName(userService.queryName(r.getCreatedBy()));
            if (StrUtil.isNotBlank(r.getUserId())) {
                r.setUserName(userService.queryName(r.getUserId()));
            }

            if (StrUtil.isNotBlank(r.getIndustryId())) {
                r.setIndustryName(industryService.queryName(r.getIndustryId()));
            }
            if (StrUtil.isNotBlank(r.getExamId())) {
                r.setExamName(examService.queryName(r.getExamId()));
            }

        }).toList();
    }

}
